package database

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
	"micro.hotel.server/config"
)

var engine *xorm.Engine

func Init() {
	cfg := config.GConf
	dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True", cfg.MySQL.User, cfg.MySQL.Password, cfg.MySQL.Host, cfg.MySQL.Port, cfg.MySQL.DBName)
	//test := fmt.Sprintf("root:123456@tcp(127.0.0.1:3306)/go-micro-hotel?charset=utf8&parseTime=True")
	eg, err := xorm.NewEngine("mysql", dns)
	if err != nil {
		panic("mysql connect error:" + fmt.Sprintf("%s", err))
	}
	engine = eg
	log.Printf("mysql connect success")
}

func GetDB() *xorm.Engine {
	return engine
}

func CloseDB() {
	engine.Close()
}
